var amapFile = require('amap-wx.js');

Page({
  data: {
    address: {},
    today: {},
    forecast: {},
    tomorrow: {},
    aftertomorrow: {},
    now: {},
    now_icon: '',
    today_icon: '',
    tomorrow_icon: '',
    aftertomorrow: '',
    imageUrl: 'image/bg.jpg',
    position: ''
  },
  onShow: function () {
    var that = this;
    var myAmapFun = new amapFile.AMapWX({
      key: getApp().data.amapKey
    });
    //获取高德定位信息
    // myAmapFun.getRegeo({
    //   success: function (data) {
    //     that.setData({
    //       address: data[0],
    //       position: data[0].longitude + ',' + data[0].latitude
    //     });
    //   },
    //   fail: function (res) {
    //     console.log('==', res)
    //   }
    // })

    wx.getFuzzyLocation({
      type: 'gcj02',
      success(res) {
        console.log(res.longitude + ',' + res.latitude);
        that.setData({
          position: res.longitude + ',' + res.latitude
        })
      },
      fail(e){
        console.log(e);
      }
    })

    setTimeout(function () {
      that.getWeatherInfo(that.data.position);
    }, 1000);
  },

  // 下拉刷新回调接口
  onPullDownRefresh: function () {
    var that = this;
    that.onShow(); //重新加载
    wx.stopPullDownRefresh(); //刷新完后收回下拉
  },
  //获取和风天气接口信息
  getWeatherInfo: function (location) {
    console.log('==', location);
    if (location == '') {
      location = "116.397441,39.909181"
    }
    var that = this;
    //需要在微信公众号的设置-开发设置中配置服务器域名
    var url = 'https://devapi.qweather.com/v7/weather/now?key=' + getApp().data.heweatherKey + '&location=' + location;
    wx.request({
      url: url,
      method: 'GET',
      success: function (res) {
        var daily = res.data.now //今天预报
        var icon = 100;
        switch (Number(daily.icon)) {
          case 150:
            icon = 100;
            break;
          case 151:
            icon = 101;
            break;
          case 152:
            icon = 102;
            break;
          case 153:
            icon = 103;
            break;
          case 154:
            icon = 104;
            break;
          case 350:
            icon = 300;
            break;
          case 351:
            icon = 301;
            break;
          case 456:
            icon = 406;
            break;
          case 457:
            icon = 407;
            break;
          default:
            icon = daily.icon;
            break;
        }
        var update = '更新于' + res.data.updateTime; //更新时间
        that.setData({
          update: update,
          now: daily,
          now_icon: 'image/weather_colorful/' + icon + '.png'
        });
      }
    })


    url = 'https://devapi.qweather.com/v7/weather/3d?key=' + getApp().data.heweatherKey + '&location=' + location;
    wx.request({
      url: url,
      data: {},
      method: 'GET',
      success: function (res) {
        var daily_forecast_today = res.data.daily[0]; //今天预报
        var daily_forecast_tomorrow = res.data.daily[1]; //明天天预报
        var daily_forecast_afterTomor = res.data.daily[2]; //后天预报
        that.setData({
          today: daily_forecast_today,
          tomorrow: daily_forecast_tomorrow,
          aftertomorrow: daily_forecast_afterTomor,
          today_icon: 'image/weather/' + daily_forecast_today.iconDay + '.png', //在和风天气中下载天气的icon图标，根据cond_code_d显示相应的图标
          tomorrow_icon: 'image/weather/' + daily_forecast_tomorrow.iconDay + '.png',
          aftertomorrow_icon: 'image/weather/' + daily_forecast_afterTomor.iconDay + '.png'
        });
      }
    })


    //隐藏更新时间
    setTimeout(function () {
      that.setData({
        'update': ''
      });
    }, 3000);
  },

})